package model

type Scene struct {
	Id                                               int
	OpenId                                           string
	Prop1, Prop2, Prop3, Prop4, Prop5                int
	Prop1Use, Prop2Use, Prop3Use, Prop4Use, Prop5Use int
	Time                                             int
	TableName                                        string
}

var (
	SceneCoin1 = [...]int{0, 90, 60, 50, 80, 70}
	SceneCoin2 = [...]int{0, 140, 110, 120, 130, 100}
	SceneCoin3 = [...]int{0, 190, 150, 160, 180, 170}
)

func (s *Scene) Create() (id int, err error) {
	sql := "INSERT INTO " + s.TableName + " (unique_id, time) VALUES (?, ?)"
	stmt, err := Db.Prepare(sql)
	if err != nil {
		return
	}
	defer stmt.Close()

	res, err := stmt.Exec(s.OpenId, s.Time)
	if err != nil {
		return
	}
	idx, err := res.LastInsertId()
	if err != nil {
		return
	}
	s.Id = int(idx)
	id = s.Id
	return
}

func (s *Scene) Get() (err error) {
	err = Db.QueryRow("SELECT id, unique_id, prop_1, prop_2, prop_3, prop_4, prop_5, prop_1_use, prop_2_use, prop_3_use,prop_4_use, prop_5_use, time FROM "+s.TableName+" WHERE id = ? limit 1", s.Id).Scan(&s.Id, &s.OpenId, &s.Prop1, &s.Prop2, &s.Prop3, &s.Prop4, &s.Prop5, &s.Prop1Use, &s.Prop2Use, &s.Prop3Use, &s.Prop4Use, &s.Prop5Use, &s.Time)
	return
}

func (s *Scene) Set(key string, val int) error {
	statement := "UPDATE " + s.TableName + " SET " + key + " = ? WHERE id = ?"
	stmt, err := Db.Prepare(statement)
	if err != nil {
		return err
	}
	defer stmt.Close()

	_, err = stmt.Exec(val, s.Id)
	return err
}
